import os
from time import time, sleep

from selenium import webdriver
from utils.configurator import config
from utils.data_path_manager import data_path_manager
from utils.logger import logger
from web_test.page.base_page import BasePage
from web_test.page.login.login_page import LoginPage
# from web_test.page.dept.dept_page import DeptPage
from web_test.page.meetingrooms.meetingrooms_page import MeetingroomsPage


class App(BasePage):
    main_page_funcs_file = os.path.join(data_path_manager.page_data_dir, "main_page_funcs.yml")

    def start(self):
        if self.driver is None:
            browser = config["driver_config"]["browser"]
            driver_config = config["driver_config"]
            if browser == "Chrome":
                options = webdriver.ChromeOptions()
                if config["proxy"]:
                    options.add_argument(f'--proxy-server={config["proxy"]}')
                if driver_config["using_headless"]:
                    options.add_argument("--headless")
                if driver_config["debugger"]["using_debugger"]:
                    options.debugger_address = driver_config["debugger"]["debugger_address"]
                self.driver = webdriver.Chrome(options=options)
            elif browser == "Firefox":
                self.driver = webdriver.Firefox()
                options = webdriver.FirefoxOptions()
                if driver_config["using_headless"]:
                    options.add_argument("--headless")
                self.driver = webdriver.Firefox(options=options)
            elif browser == "Edge":
                self.driver = webdriver.Edge()
            elif browser == "Safari":
                self.driver = webdriver.Safari()
            self.driver.maximize_window()
            self.driver.implicitly_wait(config["driver_config"]["default_implicit_waiting_time"])
            self.driver.get(config["web_url"])

        return self

    def stop(self):
        self.driver.quit()

    def current_window_handle(self):
        return self.driver.current_window_handle

    def window_handles(self):
        return self.driver.window_handles

    def current_url(self):
        return self.driver.current_url

    def switch_to_forget_username_password_window(self, window_name):
        self.driver.switch_to.window(window_name)
        return LoginPage(self.driver)

    def goto_login_page(self):
        logger.info("Goto login page")
        self.driver.get(config["web_url"] + "/index")
        return LoginPage(self.driver)

    def goto_meetingrooms_page(self):
        logger.info("Goto meetingrooms web")
        self.perform_functions(self.main_page_funcs_file, "goto_meetingrooms_page")
        return MeetingroomsPage(self.driver)

    # def goto_dept_page(self):
    #     logger.info("Goto dept page")
    #     self.perform_functions(self.main_page_funcs_file, "goto_dept_page")
    #     return DeptPage(self.driver)

    def goto_func_page(self, menu, func):
        logger.info(f"Goto {func} page")
        params = {
            "menu": menu,
            "func": func
        }
        self.perform_functions(self.main_page_funcs_file, "goto_func_page", params=params)
        return self

if __name__ == '__main__':
    dept_names = ['测试部门', '测试部门1', '测试部门2', '测试部门3', 'test', 'test1']
    app = App()
    app.driver.refresh()
    #1 click dept label
    app.click_element(('xpath', "//*[text()='部门' and @class='ant-tabs-tab']"))
    for dept_name in dept_names:
        start_time = time()
        print(dept_name)
        try:
            #2 input search keyword
            app.input_text(('xpath', "//*[@id='batch-management-wrapper']//input[@type='text']"), f"{dept_name}")
            sleep(1)
            #3 click the checkbox
            app.click_element(('xpath', f"//*[text()='{dept_name}']/..//input"))
            #4 checkbox status is checked
            app.wait_for_text_to_be_element_attribute(("xpath", f"//*[text()='{dept_name}']/..//label"), "class", "ant-checkbox-wrapper ant-checkbox-wrapper-checked")
            # app.wait_for_element_visible(("xpath", f"//*[contains(@class, '-checked')]/../..//*[text()='{dept_name}']"))
        except Exception as e:
            print(e)
        end_time = time()
        print("the wait time is %2f" % (end_time-start_time))
